function [logL_neg]=likelihood_obj(parameter,ability_metric,LowCost_Ability,HighCost_Ability,V_l,V_h,outcome,tpORtp3h,Cpm25,parameter_fixed)


N=size(LowCost_Ability,1);

% Parameters that are fixed
%cutoff_tp=parameter_fixed(1);
%cutoff_pm=parameter_fixed(2);
%delta_0  =parameter_fixed(3);

% Parameters that are estimated 
k       =parameter(1);
%delta_tp=parameter(3);
%delta_pm=parameter(4);
                
% Delta changes separately in the log of (1 +) the temperature or PM2.5 difference relative to a cutoff, i.e., temperature of 27 -> log (1 + 27 - cutoff) = log (1 + 27 - 27) = log 1 = 0; 28->.7 of delta; 43->2.8   
%delta = repmat(delta_0,N,1) + delta_tp * log( max( (tpORtp3h - repmat(cutoff_tp,N,1)) , 0) + ones(N,1)) + delta_pm * log( max( (Cpm25 - repmat(cutoff_pm,N,1)) , 0) + ones(N,1));


% Marginal cost (mc) function:
% inputs: ability metric, cost parameter, LowCost_Ability, HighCost_Ability
% outputs: c_l, c_h
[c_l,c_h]=mc(ability_metric,parameter,LowCost_Ability,HighCost_Ability);

% transition_prob_oneshot:
% inputs: c_l, c_h, V_l, V_h, k
% outputs: p_l_winoneshot=p_l_win3, p_h_winoneshot=p_h_win3
[p_lwin3,p_llose3]=transition_prob_oneshot(c_l,c_h,V_l,V_h,k);

% transition_prob_stage2:
% inputs: c_l, c_h, V_l, V_h, temperature, PM2.5, k, parameters of the delta function, calls on transition_prob_oneshot
% outputs: Continuation values (in stage 3) conditional on stage-2 outcome (and stage-1 history), stage-2 transition probabilities  
[~,~,~,~,~,~,~,~,p_lwin2_lwon1,p_llose2_lwon1,p_lwin2_llost1,p_llose2_llost1]=transition_prob_stage2(parameter,tpORtp3h,Cpm25,parameter_fixed,c_l,c_h,V_l,V_h);

% transition_prob_stage1:
% inputs: c_l, c_h, V_l, V_h, temperature, PM2.5, k, parameters of the delta function, calls on transition_prob_stage2
% outputs: Continuation values (in stage 2) conditional on stage-1 outcome, stage-1 transition probabilities   
[~,~,~,~,p_lwin1,p_llose1]=transition_prob_stage1(parameter,tpORtp3h,Cpm25,parameter_fixed,c_l,c_h,V_l,V_h);


% Compute probabilities for each match outcome (this can be sped up if necessary subsequently; e.g., for simulated likelihood)
% SELECT one set of commands to choose the stages on which to base the likelihood (ALSO change in likelihood_contrib.m):
% All three stages
prob_ll =p_lwin1 .*  p_lwin2_lwon1;
prob_lhl=p_lwin1 .* p_llose2_lwon1.* p_lwin3;
prob_lhh=p_lwin1 .* p_llose2_lwon1.*p_llose3;
prob_hll=p_llose1.* p_lwin2_llost1.* p_lwin3;
prob_hlh=p_llose1.* p_lwin2_llost1.*p_llose3;
prob_hh =p_llose1.*p_llose2_llost1;
prob=[prob_ll prob_lhl prob_lhh prob_hll prob_hlh prob_hh];
likelihood_contribution=sum(prob.*outcome,2);

%{
% Robustness: See Table 8, column 1 (and comment out the preceding paragraph of commands)
% Only stage 2 (no need to call on transition_prob_oneshot or transition_prob_stage1)
outcome_stage2=[outcome(:,1) outcome(:,2)+outcome(:,3) outcome(:,4)+outcome(:,5) outcome(:,6)];
prob_stage2=[p_lwin2_lwon1 p_llose2_lwon1 p_lwin2_llost1 p_llose2_llost1];
likelihood_contribution=sum(prob_stage2.*outcome_stage2,2);
%}    

logL_neg=-ones(1,N)*log(likelihood_contribution);


% Diagnostics (run these commands at the prompt)
%{
winProb_l_model=prob_ll+prob_lhl+prob_hll;
winProb_h_model=prob_lhh+prob_hlh+prob_hh;
winProbDiff_model=winProb_l_model-winProb_h_model;
winProbDiff_fairbet=abs(wWinProb-lWinProb)./(wWinProb+lWinProb);     % So that the sum equals 1 (account for the fact that bets are not fair) 
%scatter(winProbDiff,winProbDiff_model)
%scatter(winProbDiff_fairbet,winProbDiff_model)

% Color code according to specific environments (odds-based ability: hot=707 matches, polluted=68, mild=1382; rank-based ability: hot=878 matches, polluted=69, mild=1723)      
winProbDiff_model_mild      =winProbDiff_model(tpORtp3h <= cutoff_tp & Cpm25 <= cutoff_pm);
winProbDiff_model_hot       =winProbDiff_model(tpORtp3h > cutoff_tp);
winProbDiff_model_polluted  =winProbDiff_model(Cpm25 > cutoff_pm);
winProbDiff_fairbet_mild    =winProbDiff_fairbet(tpORtp3h <= cutoff_tp & Cpm25 <= cutoff_pm);
winProbDiff_fairbet_hot     =winProbDiff_fairbet(tpORtp3h > cutoff_tp);
winProbDiff_fairbet_polluted=winProbDiff_fairbet(Cpm25 > cutoff_pm);

% Figure A.6, panels (b) and (d)
figure;
subplot(1,1,1);
scatter(winProbDiff_fairbet_mild*100,winProbDiff_model_mild*100,'green');
hold on
scatter(winProbDiff_fairbet_hot*100,winProbDiff_model_hot*100,'red');
hold on
scatter(winProbDiff_fairbet_polluted*100,winProbDiff_model_polluted*100,'black');
hold off
title('Model-predicted against betting-market winning probability difference');
ylabel('Model-predicted winning probability difference (%)');
xlabel('Betting-market winning probability difference (%)');
legend('\leq 27 ^{\circ}C, \leq 150 \mug/m^{3}','> 27 ^{\circ}C, \leq 150 \mug/m^{3}','\leq 27 ^{\circ}C, > 150 \mug/m^{3}');
%}

end